<?php
namespace app\admin\model;
use think\Model;
use think\Db;
use think\facade\Config;
/**
 * 渠道重发模型
 * @package app\admin\model
 */
class ChannelResendConfig extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_channel_resend_config';
    

    public static function getChannelResendList($request)
    {
        $where = [];
        if ($request['cid'] != '') {
            $where[] = ['cr.cid', 'eq', $request['cid']];
        }
        return self::alias('cr')
                ->field('bp.plat_id,bp.platfrom,cr.*,bc.company_name')
                ->join('bus_platfrom bp', 'bp.plat_id = cr.plat_id')
                ->join('bus_channel bc', 'bc.channel_id = cr.cid')
                ->where($where)
                ->order('cr.cid')
                ->paginate($request['limit'], false, $request)
                ->each(function($item, $key){
                    $item->plat_id = $item->platfrom.'('. $item->plat_id .')';
                    $item->cid = $item->company_name.'('. $item->cid .')';

                    switch ($item->onoff) {
                        case 0:
                            $item->onoff = '<span class="layui-badge layui-bg-orange">已禁用</span>';
                            break;
                        case 1:
                            $item->onoff = '<span class="layui-badge layui-bg-green">已开启</span>';
                            break;

                        default:
                            $item->onoff = '';
                            break;
                    }
                    switch ($item->isp_host) {
                        case 10086:
                            $item->isp_host = '移动';
                            break;
                        case 10010:
                            $item->isp_host = '联通';
                            break;
                        case 10000:
                            $item->isp_host = '电信';
                            break;
                        case 90001:
                            $item->isp_host = '三网';
                            break;
                        default:
                            $item->isp_host = '';
                            break;
                    }
                    switch ($item->resend_type) {
                        case 0:
                            $item->resend_type = '自动补发';
                            break;
                        case 1:
                            $item->resend_type = '手动补发';
                            break;
                        default:
                            $item->resend_type = '';
                            break;
                    }
                });
                // ->toarray();
    }

    public static function createChannelResend($data)
    {
        $channelResendInfo = self::where(['isp_host' => $data['isp_host'],'cid' => $data['cid']])->find();
        if ($channelResendInfo) {
            $result = ['code' => 422, 'msg' => '该渠道存在此运营商'];
        } else {
            Db::startTrans();
            try {
                //获取通道的最大值
                $data['plat_index'] = getMaxPlatIndex($data['plat_id']) + 1;
                self::create($data);
                $result = ['code' => 200, 'msg' => '新增成功'];
                deleteCache();
                Db::commit();
            } catch (\Exception $e) {
                trace('新增渠道补发报错---' . $e->getMessage());
                // 回滚事务
                Db::rollback();
                $result = ['code' => 422, 'msg' => '新增失败'];
            }
        }
        return $result;
    }

    public static function upd($data)
    {
        $id = $data['channel_resend_config_id'];
        unset($data['channel_resend_config_id']);
        //判断通道是否改变
        $channelResendInfo = self::where(['id' => $id])->find();

        Db::startTrans();
        try {
            if ($channelResendInfo['plat_id'] != $data['plat_id']) {
                $data['plat_index'] = getMaxPlatIndex($data['plat_id']) + 1;
            }
            //获取通道的最大值
            self::where(['id' => $id])->update($data);
            $result = ['code' => 200, 'msg' => '修改成功'];
            Db::commit();
            deleteCache();
        } catch (\Exception $e) {
            trace('新增渠道补发报错---' . $e->getMessage());
            if (strpos($e->getMessage(),'for key') !== false) {
                $result = ['code' => 422, 'msg' => '该渠道存在此运营商'];
            } else {
                $result = ['code' => 422, 'msg' => '修改失败'];
            }
            // 回滚事务
            Db::rollback();
        }
        return $result;
    }
}
